/**
 * @公司:公约树
 * @位置:苏州
 * @Classname Anime02login
 * @Date 2022-06-11 上午 12:01
 * @Created by hanWanLu
 * @auther 中文:韩万路
 */


package com.animeJavaExample;

import com.animeJava.core.AnimeJava;
import com.animeJava.core.EasingData;
import com.animeJava.dao.animeEvent.AnimeRender;
import com.animeJava.mode.PositionVarModel;
import com.animeJava.mode.animeModel.AnimeProperty;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

import java.util.List;
import java.util.Map;


/**
 * 示例动画 登录框
 */
public class Anime02login extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.initStyle(StageStyle.TRANSPARENT);
        VBox root = createRoot();
        Scene scene = new Scene(root);
        scene.setFill(null);
        stage.setScene(scene);

        //创建登录展示框
        AnchorPane loginBox = createLoginBox();
        root.getChildren().add(loginBox);
        loginBox.setTranslateX(-100);
        loginBox.setOpacity(0);

        Button btn_create = createButton("   去注册   ");
        loginBox.getChildren().add(btn_create);
        AnchorPane.setBottomAnchor(btn_create , 50D);
        AnchorPane.setLeftAnchor(btn_create , 40D);

        Button btn_login = createButton("   去登录   ");
        loginBox.getChildren().add(btn_login);
        AnchorPane.setBottomAnchor(btn_login , 50D);
        AnchorPane.setRightAnchor(btn_login , 40D);

        //创建登录输入box
        AnchorPane inputBox = createInputBox();
        loginBox.getChildren().add(inputBox);
        inputBox.setLayoutX(270);
        inputBox.setTranslateX(200);

        VBox inputLoginBox = createInputLoginBox();
        inputBox.getChildren().add(inputLoginBox);
        inputLoginBox.setLayoutX(40);

        VBox inputCreateBox = createInputCreateBox();
        inputBox.getChildren().add(inputCreateBox);
        inputCreateBox.setLayoutX(40);
        inputCreateBox.setOpacity(0);
        inputCreateBox.setDisable(true);


        AnimeProperty initAnimeProperty = new AnimeProperty();
        initAnimeProperty.setEasing(new EasingData(0.83,0.00,0.16,1.00));
        initAnimeProperty.setDuration(1200);
        initAnimeProperty.setAutoplay(true);
        initAnimeProperty.setDelay(0);
        initAnimeProperty.addKeyFrames(0,0D,0D);
        initAnimeProperty.addKeyFrames(90,null,1D);
        initAnimeProperty.addKeyFrames(100,1D,1D);
        AnimeJava animeJava1 = new AnimeJava(initAnimeProperty);
        animeJava1.update(new AnimeRender() {
          @Override
          public boolean run(Double timeTo, Map<String, PositionVarModel> animeInfo, List<Double> data) {
            if(data.get(0) != null){
                Platform.runLater(()->{
                    double v = 1 - data.get(0);
                    loginBox.setTranslateX(v * -100);
                    inputBox.setTranslateX(v * 200);
                    loginBox.setOpacity(data.get(0));
                });
            }
            if(data.get(1) != null){
//                Platform.runLater(()-> {
//                });
            }
            return (loginBox.getScene() == null)?true:false;
        }});
        animeJava1.play();

        AnimeProperty animeProperty = new AnimeProperty();
        animeProperty.setEasing(new EasingData(0.83,0.00,0.16,1.00));
        animeProperty.setDuration(1200);



        animeProperty.addKeyFrames(0,270D ,0D);
        animeProperty.addKeyFrames(100,30D ,1D);

        AnimeJava animeJava = new AnimeJava(animeProperty);
        animeJava.update(new AnimeRender() {
            String timeToData = "0.0";
            @Override
            public boolean run(Double timeTo, Map<String, PositionVarModel> animeInfo, List<Double> data) {
                if(data.get(0) != null){
                    Platform.runLater(()-> {
                        inputBox.setLayoutX(data.get(0) );
                    });
                }
                if(data.get(1) != null){
                    Platform.runLater(()-> {
                        inputLoginBox.setLayoutX(40+(data.get(1)*40   ) );
                        inputLoginBox.setOpacity(1-data.get(1));
                        inputLoginBox.setDisable(!"0.0".equals(String.format("%.1f", data.get(1)))  ?true:false);
                        inputCreateBox.setLayoutX((data.get(1)*40   ) );
                        inputCreateBox.setOpacity(data.get(1));
                        inputCreateBox.setDisable("0.0".equals(String.format("%.1f", data.get(1)))  ?true:false);
                    });
                }
                return (inputCreateBox.getScene() == null)?true:false;
            }
        });
        btn_create.setOnAction(event -> animeJava.play());
        btn_login.setOnAction(event -> animeJava.playFlashback());

        stage.show();
        scene.setFill(null);
        stage.setOnHiding(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
//                animeJava.close();
            }
        });
    }


    /**
     * 创建主要框
     * @return
     */
    public VBox createRoot(){
        VBox root = new VBox();
        root.setMinWidth(800);
        root.setMinHeight(600);
        root.setStyle("-fx-background-color: rgba(255,255,255,0);");
        root.setAlignment(Pos.CENTER);

        return root;
    }

    /**
     * 创建登录box
     * @return
     */
    public AnchorPane createLoginBox(){
        AnchorPane anchorPane = new AnchorPane();

        anchorPane.setMinWidth(600);
        anchorPane.setMaxWidth(600);
        anchorPane.setMinHeight(400);
        anchorPane.setMaxHeight(400);

        anchorPane.setStyle("-fx-background-color: rgb(238,238,238); -fx-background-radius: 10px; -fx-border-color: #9a9a9a ; -fx-border-radius: 10px ");

        return anchorPane;
    }

    /**
     *  创建输入的盒子
     * @return  返回布局类型
     */
    public AnchorPane createInputBox(){
        AnchorPane anchorPane = new AnchorPane();

        anchorPane.setMinWidth(300);
        anchorPane.setMaxWidth(300);
        anchorPane.setMinHeight(300);
        // anchorPane.setMaxHeight(600);
        AnchorPane.setTopAnchor(anchorPane,-14D);
        AnchorPane.setBottomAnchor(anchorPane,-14D);
//        anchorPane.setBackground(new Background(new BackgroundFill(new Color(0.96,0.42,0.42 , 1) , new CornerRadii(10), null)));
//        anchorPane.setBorder(new Border(new BorderStroke(new Color(0.96,0.42,0.42 , 0.6) , BorderStrokeStyle.SOLID , new CornerRadii(10) ,new BorderWidths(1) )));
        anchorPane.setStyle("-fx-background-color: #ffe3ed; -fx-background-radius: 10px ; -fx-border-color: #ffabc0; -fx-border-radius: 10px");
//        anchorPane.setBackground(new Background(new BackgroundFill(new Color(0.70,0.50,0.50 , 1) , new CornerRadii(10), null)));
//        anchorPane.setBorder(new Border(new BorderStroke(new Color(0.96,0.42,0.42 , 0.6) , BorderStrokeStyle.SOLID , new CornerRadii(10) ,new BorderWidths(1) )));

        DropShadow dropShadow = new DropShadow();
        dropShadow.setRadius(10);
        dropShadow.setColor(Color.valueOf("#000c"));
        dropShadow.setHeight(10);
        anchorPane.setEffect(dropShadow);


        AnimeProperty animeProperty = new AnimeProperty();
        animeProperty.setEasing(new EasingData(0.83,0.00,0.16,1.00));
        animeProperty.setDuration(350);
        animeProperty.addKeyFrames(0, dropShadow.getHeight());
        animeProperty.addKeyFrames(100,20D);

        animeProperty.setBegin((msg) -> {
            System.out.println("动画开始:"+msg);
        });
        animeProperty.setComplete(((msg) -> {
            System.out.println("动画结束:"+msg);
        }));

        AnimeJava animeJava = new AnimeJava(animeProperty);
        animeJava.update(new AnimeRender() {
            @Override
            public boolean run(Double timeTo, Map<String, PositionVarModel> animeInfo, List<Double> data) {
                if(data.get(0) != null){
                    Platform.runLater(()->{
                        dropShadow.setRadius(data.get(0));
                        dropShadow.setHeight(data.get(0));
                    });
                }
                return (anchorPane.getScene() == null)?true:false;
            }
        });
        anchorPane.setOnMouseEntered(event -> animeJava.play());
        anchorPane.setOnMouseExited(event -> animeJava.playFlashback());

        anchorPane.setUserData(animeProperty);

        return anchorPane;
    }

    /**
     * 创建按钮
     * @param text  按钮文字
     * @return 返回按钮对象
     */
    public Button createButton(String text){
        Button button = new Button(text);

        button.setPadding(new Insets(4,10,4,10));
        button.setTextFill(new Color(0.96,0.42,0.42 , 1));
        button.setBackground(new Background(new BackgroundFill(new Color(0.96,0.42,0.42 , 0.1) , new CornerRadii(10), null)));
        button.setBorder(new Border(new BorderStroke(new Color(0.96,0.42,0.42 , 1) , BorderStrokeStyle.SOLID , new CornerRadii(10) ,new BorderWidths(1) )));
        button.setCursor(Cursor.HAND);

        AnimeProperty animeProperty = new AnimeProperty();
        animeProperty.setDuration(200);
        animeProperty.addKeyFrames(0,0.96,0.42,0.42  , 0.1 , 1D);
        animeProperty.addKeyFrames(100,1D,1D,1D,1D , 0.1);
        animeProperty.setEasing(new EasingData());

        AnimeJava animeJava = new AnimeJava(animeProperty);
        animeJava.update((timeTo, animeInfo, data) -> {
            if(data.get(0) != null && data.get(1) != null && data.get(2) != null )
                Platform.runLater(()->button.setTextFill(new Color(data.get(0),data.get(1),data.get(2) , 1)) );

            if(data.get(3) != null)
                Platform.runLater(()->button.setBackground(new Background(new BackgroundFill(new Color(0.96,0.42,0.42 , data.get(3)) , new CornerRadii(10), null)))  );

            if(data.get(4) != null)
                Platform.runLater(()->button.setBorder(new Border(new BorderStroke(new Color(0.96,0.42,0.42 , data.get(4)) , BorderStrokeStyle.SOLID , new CornerRadii(10) ,new BorderWidths(1) )))  );
            return (button.getScene() == null)?true:false;
        });
        button.setOnMouseEntered(event -> animeJava.play());
        button.setOnMouseExited(event -> animeJava.playFlashback());

        return button;
    }
    /**
     * 创建输入框
     * @param text  文字
     * @return 返回输入框
     */
    public TextField createTextField(String text){
        TextField textField = new TextField(text);

        textField.setAlignment(Pos.CENTER);

        textField.setPadding(new Insets(4,10,4,10));
        Font font = new Font("微软雅黑",16);
        textField.setStyle("-fx-text-fill: #fff");
        textField.setFont(font);
        textField.setBackground(new Background(new BackgroundFill(new Color(0.96,0.42,0.42 , 0.1) , new CornerRadii(10), null)));
        textField.setBorder(new Border(new BorderStroke(new Color(0.96,0.42,0.42 , 1) , BorderStrokeStyle.SOLID , new CornerRadii(10) ,new BorderWidths(1) )));
        textField.setCursor(Cursor.HAND);

        AnimeProperty animeProperty = new AnimeProperty();
        animeProperty.setDuration(200);
        animeProperty.addKeyFrames(0,0.96,0.42,0.42  , 0.1 , 1D);
        animeProperty.addKeyFrames(100,1D,1D,1D,1D , 0.1);
        animeProperty.setEasing(new EasingData());

        AnimeJava animeJava = new AnimeJava(animeProperty);
        animeJava.update((timeTo, animeInfo, data) -> {
            if(data.get(0) != null && data.get(1) != null && data.get(2) != null )
//                textField.setTextFill(new Color(data.get(0),data.get(1),data.get(2) , 1));
                if(data.get(3) != null){
                    Platform.runLater(()->textField.setBackground(new Background(new BackgroundFill(new Color(0.96,0.42,0.42 , data.get(3)) , new CornerRadii(10), null)))  );
                }
            if(data.get(4) != null){
                Platform.runLater(()->textField.setBorder(new Border(new BorderStroke(new Color(0.96,0.42,0.42 , data.get(4)) , BorderStrokeStyle.SOLID , new CornerRadii(10) ,new BorderWidths(1) )))  );
            }
            return (textField.getScene() == null)?true:false;
        });

        textField.setOnMouseEntered(event -> animeJava.play());
        textField.setOnMouseExited(event -> animeJava.playFlashback());

        return textField;
    }


    /**
     * 创建输入登录盒子
     * @return
     */
    public VBox createInputLoginBox(){
        VBox anchorPane = new VBox();
        anchorPane.setLayoutY(20);
        anchorPane.setMinWidth(220);
        anchorPane.setMaxWidth(220);
        anchorPane.setAlignment(Pos.TOP_CENTER);

        HBox hBox = new HBox();
        anchorPane.getChildren().add(hBox);
        hBox.setAlignment(Pos.TOP_LEFT);
        Label label = new Label("登录");
        hBox.getChildren().add(label);
        label.setTextFill(Paint.valueOf("#F56B6BFF"));
        label.setFont(new Font(24));
        label.setLayoutX(20);

        TextField userInfo = createTextField(null);
        anchorPane.getChildren().add(userInfo);
        anchorPane.setMargin(userInfo,new Insets(40,0,0,0));

        TextField password = createTextField(null);
        anchorPane.getChildren().add(password);
        anchorPane.setMargin(password,new Insets(20,0,0,0));


        Button button = createButton("       登录       ");
        button.setFont(new Font(16));
        anchorPane.getChildren().add(button);
        anchorPane.setMargin(button,new Insets(100,0,0,0));

        return anchorPane;
    }
    /**
     * 创建输入注册盒子
     * @return
     */
    public VBox createInputCreateBox(){
        VBox anchorPane = new VBox();
        anchorPane.setLayoutY(20);
        anchorPane.setMinWidth(220);
        anchorPane.setMaxWidth(220);
        anchorPane.setAlignment(Pos.TOP_CENTER);
//        anchorPane.setStyle("-fx-border-color: #00ff15");

        HBox hBox = new HBox();
        anchorPane.getChildren().add(hBox);
        hBox.setAlignment(Pos.TOP_LEFT);
        Label label = new Label("注册");
        hBox.getChildren().add(label);
        label.setTextFill(Paint.valueOf("#F56B6BFF"));
        label.setFont(new Font(24));
        label.setLayoutX(20);

        TextField userInfo = createTextField(null);
        anchorPane.getChildren().add(userInfo);
        anchorPane.setMargin(userInfo,new Insets(40,0,0,0));

        TextField password = createTextField(null);
        anchorPane.getChildren().add(password);
        anchorPane.setMargin(password,new Insets(20,0,0,0));


        Button button = createButton("       注册       ");
        button.setFont(new Font(16));
        anchorPane.getChildren().add(button);
        anchorPane.setMargin(button,new Insets(100,0,0,0));



        return anchorPane;
    }
}
